<?php
namespace app\admin\controller;

use app\model\admin;
use think\Request;
use think\Session;
use think\Controller;
use think\Db;


class Agent extends Controller
{
    //代理列表
    public function index()
    {
        $agent_levels = Db::name('agent_level')->order($sort='id desc')->select();
        $where = [];//自 5.4 起可以使用短数组定义语法，用 [] 替代 array()
        $name = $this->request->get("name");
        $level_id = $this->request->get("level_id");
        if(!empty($name)){
            $where['name'] = ['like','%'.$name.'%'];
        }
        if(!empty($level_id)){
            $where['level_id'] = $level_id;
        }

        $join = [
            ['lh_agent_level b','a.level_id=b.id'],
        ];
        $lists = Db::name('agent')->alias('a')->where($where)->join($join)->field('a.id,a.name,a.phone,a.weixin,a.status,a.view,a.e_time,a.level_id,b.title')->order($sort='id desc')->paginate(20);


        $this->assign('lists', $lists);
        $this->assign('c_lists', $agent_levels);
        return $this->fetch('agent/index');
    }
    //文章详情
    public function detail()
    {
        $agent_levels = Db::name('agent_level')->order($sort='id desc')->select();
        $id = $request = $this->request->get('id');
        if(empty($id)){
            $data = array();
        }else{
            $data = Db::name('agent')->where('id',$id)->find();
        }
        $this->assign('agent_levels', $agent_levels);
        return $this->fetch('agent/detail',$data);
    }

    //代理保存修改
    public function agent_save()
    {
        $data['id'] = (int)$this->request->post('id');
        $data['name'] = deletehtml($this->request->post('name'));
        $data['level_id'] = (int)$this->request->post('level_id');
        $data['phone'] = deletehtml($this->request->post('phone'));
        $data['weixin'] = deletehtml($this->request->post('weixin'));
        $data['view'] = deletehtml($this->request->post('view'));
        $data['s_time'] = strtotime($this->request->post('s_time'));
        $data['e_time'] = strtotime($this->request->post('e_time'));
        $data['remarks'] = deletehtml($this->request->post('remarks'));
        $data['status'] = (int)$this->request->post('status');

        //判断栏目是否存在
        $id = $request = $this->request->get('id');
        $c_info = Db::name('agent_level')->where('id',$data['level_id'])->find();
        if(!$c_info){
            $this->error('栏目不存在！', '/admin/agent/detail?id='.$data['id']);
        }
        if(empty($data['id'])){
            $agent_status = Db::name('agent')->insertGetId($data);;
        }else{
            $data['upd_time'] = time();
            $where['id'] = $data['id'];
            $agent_status = Db::name('agent')->where($where)->update($data);
        }
        if($agent_status){
            $this->redirect('/admin/agent/index?');
        }else{
            $this->error('操作失败！', '/admin/agent/detail?id='.$data['id']);
        }
    }

    public function agent_status(){
        if($this->request->get("id")<1){
            return json(['status'=>2,'data'=>(object)[],'msg'=>'参数错误!']);
        }else{
            $id = $this->request->get("id");
        }
        $status = $this->request->get("status");
        if(!in_array($status,[1,2]))
        {
            return json(['status'=>2,'data'=>(object)[],'msg'=>'参数错误!']);
        }
        $where['id'] = $id;
        $data['status'] = $status;
        $status = Db::name('agent')->where($where)->update($data);
        if($status > 0){
            return json(['status'=>0,'data'=>(object)[],'msg'=>'操作成功!']);
        }else{
            return json(['status'=>3,'data'=>(object)[],'msg'=>'操作失败!']);
        }
    }

    //代理删除
    public function agent_delete()
    {
        if($this->request->get("id")<1){
            return json(['status'=>2,'data'=>(object)[],'msg'=>'参数错误!']);
        }else{
            $id = $this->request->get("id");
        }
        $agent_delete = Db::name('agent')->delete([$id]);
        if($agent_delete == 0){
            return json(['status'=>3,'data'=>(object)[],'msg'=>'删除失败!']);
        }else{
            return json(['status'=>0,'data'=>$agent_delete,'msg'=>'操作成功!']);
        }
    }
    //代理等级列表
    public function agent_level()
    {
        $agent_levels = Db::name('agent_level')->order($sort='id desc')->select();
        $this->assign('lists', $agent_levels);
        return $this->fetch('agent/agent_level');
    }
    //代理等级详情
    public function agent_level_detail()
    {
        $id = $request = $this->request->get('id');
        if(empty($id)){
            $data = array();
        }else{
            $data = Db::name('agent_level')->where('id',$id)->find();
        }
        return $this->fetch('agent/agent_level_detail',$data);
    }

    //代理级别保存修改
    public function agent_level_save()
    {
        $data['id'] = deletehtml($this->request->post('id'));
        $data['title'] = deletehtml($this->request->post('title'));
        $data['level'] = (int)$this->request->post('level');
        $data['add_time'] = time();
        //判断栏目是否存在
        $cdao = new \app\model\admin\Column();
        $id = $request = $this->request->get('id');

        if(empty($data['id'])){
            $status = Db::name('agent_level')->insertGetId($data);
        }else{
            $where['id'] = $data['id'];
            $status = Db::name('agent_level')->where($where)->update($data);
        }
        if($status){
            $this->redirect('/admin/agent/agent_level?');
        }else{
            $this->error('操作失败！', '/admin/agent_level/detail?id='.$data['id']);
        }
    }
    //代理级别删除
    public function agent_level_del()
    {
        if($this->request->get("id")<1){
            return json(['status'=>2,'data'=>(object)[],'msg'=>'参数错误!']);
        }else{
            $id = $this->request->get("id");
        }

        $del_status = Db::name('agent_level')->delete([$id]);
        if($del_status == 0){
            return json(['status'=>3,'data'=>(object)[],'msg'=>'删除失败!']);
        }else{
            return json(['status'=>0,'data'=>$del_status,'msg'=>'操作成功!']);
        }
    }
}


